package com.example.controller;

import com.example.common.Code;
import com.example.common.Result;
import com.example.domain.Candidate;
import com.example.service.CandidateService;
import com.example.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author luo
 * 应聘者管理
 */
@RestController
@RequestMapping("/candidates")
@Slf4j
public class CandidateController{

    @Autowired
    private CandidateService candidateService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public Result selectCandidate(){
        List<Candidate> candidates = candidateService.selectCandidate();
        String msg = candidates!=null ? "" : "数据查询失败，请重试！";
        Integer code = candidates != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,candidates,msg);
    }
    @GetMapping("/{id}")
    public Result selectCandidate(@PathVariable Integer id){
        Candidate candidates = null;

        String key = "Candidate_ID：" +id;
        //先从redis中获取缓存数据
        candidates = (Candidate) redisTemplate.opsForValue().get(key);

        if(candidates != null){
            //如果存在，直接返回，无需查询数据库
            return new Result(Code.GET_OK,candidates,"查询成功！");
        }
        candidates = candidateService.selectCandidateById(id);
        redisTemplate.opsForValue().set(key,candidates,60, TimeUnit.MINUTES);

        String msg = candidates!=null ? "" : "数据查询失败，请重试！";
        Integer code = candidates != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,candidates,msg);
    }

    @PostMapping("/ByName")
    public Result selectCandidateByName(@RequestBody Candidate candidate){
        List<Candidate> candidates = candidateService.selectCandidateByName(candidate);
        String msg = candidates!=null ? "" : "数据查询失败，请重试！";
        Integer code = candidates != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,candidates,msg);
    }

    @PostMapping
    public Result addCandidate(@RequestBody Candidate candidate){
        int i = candidateService.addCandidate(candidate);
        String msg = i > 0 ? "" : "数据添加失败，请重试！";
        if (i > 0) {
            System.out.println("插入成功");
            return new Result(Code.INSERT_OK,i,msg);
        } else {
            System.out.println("插入失败");
            return new Result(Code.INSERT_ERR,i,msg);
        }
    }
    @PutMapping
    public Result updateCandidate(@RequestBody Candidate candidate) {
        int i = candidateService.updateCandidate(candidate);
        String msg = i > 0 ? "" : "数据更新失败，请重试！";

        //删除redis中id缓存
        String key = "Candidate_ID：" +candidate.getId();
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("更新成功");
            return new Result(Code.UPDATE_OK, i, msg);
        } else {
            System.out.println("更新失败");
            return new Result(Code.UPDATE_ERR, i, msg);
        }
    }
    @DeleteMapping("/{id}")
    public Result deleteDepartment(@PathVariable Integer id){
        int i = candidateService.deleteCandidate(id);
        String msg = i > 0 ? "" : "数据删除失败，请重试！";

        //删除redis中id缓存
        String key = "Candidate_ID：" +id;
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("删除成功");
            return new Result(Code.DELETE_OK, i, msg);
        } else {
            System.out.println("删除失败");
            return new Result(Code.DELETE_OK, i, msg);
        }
    }
    @GetMapping("/{page}/{size}")
    public Result findUserList(@PathVariable int page,@PathVariable int size) {
        PageInfo<Candidate> page1 = candidateService.findPage(page, size);

        log.error(String.valueOf(page1.getList()));
        Integer code = page1 != null ? Code.GET_OK : Code.GET_ERR;
        String msg = page1 != null ? "" : "分页失败，请重试！";
        return new Result(code, page1, msg);
    }
    /**
     * 导出excel表
     */

    @RequestMapping("/makeExcel")
    public void makeExcel(HttpServletResponse response) throws Exception {
        //打印Excel表
        ExcelUtils.exportExcel(response, "candidate.xlsx", candidateService.getOutputExcelProjectData());
    }
}
